//
//  RecreationViewController.m
//  HappyNews
//
//  Created by lanou on 16/1/11.
//  Copyright © 2016年 BPWBANP. All rights reserved.
//

#import "RecreationViewController.h"

#import "RecreationCollectionViewCell.h"

#import "RecreationHeaderCollectionReusableView.h"

#import "HNRequestManager.h"

#import "RecreationViewModel.h"

#import "DCPicScrollView.h"

#import "DCWebImageManager.h"

#define recreationURL @"http://vcsp.ifeng.com/vcsp/appData/recommendGroupByTeamid.do?channelId=100391-0&showType=double&useType=iPhone&positionId=&adapterNo=6.11.1&isNotModified=0"

@interface RecreationViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *recreationCollection;

@property (nonatomic, strong) NSMutableArray *headerArray;

@property (nonatomic, strong) NSMutableArray *titleArray;

@property (nonatomic, strong) NSMutableArray *imageArray;

@property (nonatomic, strong) NSMutableArray *receationCellArr;

@property (nonatomic, strong) RecreationHeaderCollectionReusableView *header;




@end

@implementation RecreationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addRecreationConnection];
    
    [self setRecreationUpData];
    
    _titleArray = [NSMutableArray array];
    
    _imageArray = [NSMutableArray array];
    
    _headerArray = [NSMutableArray array];
    
    _receationCellArr = [NSMutableArray array];
    
}

- (void)setRecreationUpData
{
    [HNRequestManager requestWithUrlString:recreationURL parDic:nil method:GET finish:^(NSData *data) {
     
        NSDictionary *RecreationDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
      
        NSArray *headerArray = RecreationDic[@"header"];
       
        for (NSMutableDictionary *headerDic in headerArray) {
           
            RecreationViewModel *model = [[RecreationViewModel alloc]init];
           
            [model setValuesForKeysWithDictionary:headerDic];
           
            [_titleArray addObject:model.title];
           
            [_imageArray addObject:model.image];
           
            [_headerArray addObject:model];
        }
       
        
        
        NSArray *bodyListArray = RecreationDic[@"bodyList"];
        
        for (NSMutableDictionary *receationOneDic in bodyListArray) {
            
            NSArray *videoListArray = receationOneDic[@"videoList"];
            
            for (NSMutableDictionary *receationTwoDic in videoListArray) {
                RecreationViewModel *model = [[RecreationViewModel alloc]init];
                
                [model setValuesForKeysWithDictionary:receationTwoDic];
                
                [_receationCellArr addObject:model];
            }
        
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
           [_recreationCollection reloadData];
        });
        
    } error:^(NSError *error) {
        
    }];
    
    
    
}
- (void)addRecreationConnection
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    
    layout.minimumInteritemSpacing = 10;
    
    layout.minimumLineSpacing = 10;
    
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    layout.itemSize = CGSizeMake((kScreenWidth - 30) / 2, kScreenHeight / 3.7);
    
    layout.headerReferenceSize = CGSizeMake(kScreenWidth, kScreenHeight / 3);
    
    _recreationCollection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 155) collectionViewLayout:layout];
    
    _recreationCollection.delegate = self;
    
    _recreationCollection.dataSource = self;
    
    _recreationCollection.backgroundColor = [UIColor whiteColor];
    
    [_recreationCollection registerClass:[RecreationCollectionViewCell class] forCellWithReuseIdentifier:@"RecreationCollectionViewCell"];
    
    [_recreationCollection registerClass:[RecreationHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"RecreationHeaderCollectionReusableView"];
    
    [self.view addSubview:_recreationCollection];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _receationCellArr.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    _header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"RecreationHeaderCollectionReusableView" forIndexPath:indexPath];
    
    _header.titleArray = _titleArray;
    
    _header.imageArray = _imageArray;
    
    _header.headerArray = _headerArray;
    
    __weak NSArray *myheadArray = _headerArray;
    
    __weak id myDelegate = _delegate;
    
    [_header.picView setImageViewDidTapAtIndex:^(NSInteger index) {
        
        
        if (myDelegate && [myDelegate respondsToSelector:@selector(addRecreationHeaderNav:)]) {
            [myDelegate addRecreationHeaderNav:myheadArray[index]];
        }
        
    }];
    
    return _header;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RecreationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RecreationCollectionViewCell" forIndexPath:indexPath];
    
    cell.model = _receationCellArr[indexPath.row];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (_delegate && [_delegate respondsToSelector:@selector(addRecreationHeaderNav:)]) {
        [_delegate addRecreationHeaderNav:_receationCellArr[indexPath.row]];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
